#! /usr/bin/env python
# -*- coding: utf-8 -*-

import Tkinter as tk
import PyTkinter as pytk
from tkFileDialog import askopenfilename
from tkFileDialog import askdirectory
import os

# set size
import Adaptive
size_dict = Adaptive.size_dict

class PyinstallerUI(object):
    def __init__(self, master=None):
        self.root = master
        self.create_frame()

    def create_frame(self):
        '''
        新建窗口，分为上下2个部分，下半部分为状态栏
        '''
        self.frm = pytk.PyLabelFrame(self.root, 
                                     text="", 
                                     bg="#292929", 
                                     fg="#1E90FF")
        self.frm_status = pytk.PyLabelFrame(self.root, 
                                            text="", 
                                            bg="#292929", 
                                            fg="#1E90FF")

        self.frm.grid(row=0, column=0, sticky="wesn")
        self.frm_status.grid(row=1, column=0, sticky="wesn")

        self.create_frm()
        self.create_frm_status()

    def create_frm(self):
        '''
        上半部分窗口分为上中2个部分加一个按钮
        '''
        self.frm_top = pytk.PyLabelFrame(self.frm)
        self.frm_middle = pytk.PyLabelFrame(self.frm)
        self.frm_btn_generate = pytk.PyButton(self.frm, 
                                              text="Generate",
                                              font=("Monaco", 12),
                                              command=self.Generate)

        self.frm_top.grid(row=0, column=0, padx=5, pady=5, sticky="wesn")
        self.frm_middle.grid(row=1, column=0, padx=5, pady=5, sticky="wesn")
        self.frm_btn_generate.grid(row=2, column=0, padx=5, pady=5, sticky="wesn")

        self.create_frm_top()
        self.create_frm_middle()

    def create_frm_top(self):
        '''
        上半部分顶部窗口：
        1、选择源文件
        2、选择工作路径（中间文件）
        3、选择生成exe的路径
        4、选择ico的路径
        '''
        label_text_list = ["Source File:", 
                           "Work Path:", 
                           "Dist Path:", 
                           "ICO File:"]

        self.entry_value_list = list()
        for index,label_text in enumerate(label_text_list):
            temp_strvar = tk.StringVar()
            temp_label = pytk.PyLabel(self.frm_top, 
                                      font=("Monaco", 12), 
                                      text=label_text)
            temp_entry = pytk.PyEntry(self.frm_top, 
                                      width=size_dict["path_entry_width"], 
                                      font=("Monaco", 12), 
                                      textvariable=temp_strvar)
            self.entry_value_list.append(temp_strvar)
            temp_label.grid(row=index%4, column=0, padx=5, pady=5, sticky="w")
            temp_entry.grid(row=index%4, column=1, padx=5, pady=5, sticky="w")

        self.frm_top_btn_source_file = pytk.PyButton(self.frm_top, 
                                       text="...", 
                                       width=size_dict["path_btn_width"], 
                                       font=("Monaco", 8),
                                       command=self.ChooseSourceFile)
        self.frm_top_btn_work_path = pytk.PyButton(self.frm_top, 
                                       text="...", 
                                       width=size_dict["path_btn_width"], 
                                       font=("Monaco", 8),
                                       command=self.ChooseWorkPath)
        self.frm_top_btn_dist_path = pytk.PyButton(self.frm_top, 
                                       text="...", 
                                       width=size_dict["path_btn_width"], 
                                       font=("Monaco", 8),
                                       command=self.ChooseDistPath)
        self.frm_top_btn_ico_file = pytk.PyButton(self.frm_top, 
                                       text="...", 
                                       width=size_dict["path_btn_width"], 
                                       font=("Monaco", 8),
                                       command=self.ChooseICOFile)
        self.frm_top_btn_source_file.grid(row=0, column=2, padx=5, pady=5, sticky="w")
        self.frm_top_btn_work_path.grid(row=1, column=2, padx=5, pady=5, sticky="w")
        self.frm_top_btn_dist_path.grid(row=2, column=2, padx=5, pady=5, sticky="w")
        self.frm_top_btn_ico_file.grid(row=3, column=2, padx=5, pady=5, sticky="w")

    def create_frm_middle(self):
        '''
        常用的三个附加属性选择
        '''
        self.onefile = tk.IntVar()
        self.noconsole = tk.IntVar()
        self.rename_exe = tk.IntVar()
        self.rename_strvar = tk.StringVar()
        self.frm_middle_cbtn_isonefile = pytk.PyCheckbutton(self.frm_middle,
                                         text="onefile",
                                         variable=self.onefile,
                                         font=("Monaco", 15),
                                         relief="flat")
        self.frm_middle_cbtn_noconsole = pytk.PyCheckbutton(self.frm_middle,
                                         text="noconsole",
                                         variable=self.noconsole,
                                         font=("Monaco", 15),
                                         relief="flat")
        self.frm_middle_cbtn_rename_exe = pytk.PyCheckbutton(self.frm_middle,
                                          text="rename_exe",
                                          variable=self.rename_exe,
                                          font=("Monaco", 15),
                                          relief="flat")
        self.frm_middle_entry_rename = pytk.PyEntry(self.frm_middle,
                                       width=size_dict["rename_entry_width"],
                                       font=("Monaco", 15), 
                                       textvariable=self.rename_strvar)
        self.frm_middle_cbtn_isonefile.grid(row=0, column=0, padx=5, pady=5, sticky="w")
        self.frm_middle_cbtn_noconsole.grid(row=0, column=1, padx=5, pady=5, sticky="w")
        self.frm_middle_cbtn_rename_exe.grid(row=0, column=2, padx=5, pady=5, sticky="w")
        self.frm_middle_entry_rename.grid(row=0, column=3, padx=5, pady=5, sticky="w")     

        self.onefile.set(1)
        self.noconsole.set(1)

    def create_frm_status(self):
        '''
        下半部分状态栏窗口
        '''
        self.frm_status_label = pytk.PyLabel(self.frm_status, 
                                             text="Ready",
                                             font=("Monaco", 12))
        self.frm_status_label.grid(row=1, column=0, padx=5, pady=5, sticky="wesn")

    def Generate(self):
        '''
        生成可执行文件
        '''
        pass

    def ChooseSourceFile(self):
        '''
        选择源文件
        '''
        source_file = askopenfilename(filetypes=(("py files", "*.py"),
                                                ("All files", "*.*")))
        if not source_file:
            return
        default_path = os.path.dirname(source_file)
        self.entry_value_list[0].set(source_file)
        self.entry_value_list[1].set(default_path)
        self.entry_value_list[2].set(default_path)

    def ChooseWorkPath(self):
        '''
        选择工作路径（中间文件存放的文件夹）
        '''
        work_path = askdirectory()
        if not work_path:
            return
        self.entry_value_list[1].set(work_path)

    def ChooseDistPath(self):
        '''
        选择生成文件路径（exe）
        '''
        dist_path = askdirectory()
        if not dist_path:
            return
        self.entry_value_list[2].set(dist_path)

    def ChooseICOFile(self):
        '''
        选择ico文件
        '''
        ico_file = askopenfilename(filetypes=(("ico files", "*.ico"),
                                             ("All files", "*.*")))
        if not ico_file:
            return
        self.entry_value_list[3].set(ico_file)

if __name__ == '__main__':
    '''
    main loop
    '''
    root = tk.Tk()
    root.title("Pyinstaller Tool")
    PyinstallerUI(master=root)
    root.resizable(False, False)
    root.mainloop()
